package com.example.spring;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class DemoAutoCrawler extends BreadthCrawler  {
	 HashMap <String,ArrayList<String>>map=new HashMap<>();
	public DemoAutoCrawler(String carwlPath,boolean autoParse)
	{
		super(carwlPath,autoParse);
		//无法正常支持\?转义
		for(int page=78679992;page<78679992+10;page++)
		this.addSeed(String.format("https://300report.jumpw.com/match.html?id=%d",page));
		this.addRegex("https://300report.jumpw.com/match.html[?]id=[0-9]{8}");
	    this.addRegex("-.*\\.(jpg|png|gif).*");
	    this.addRegex("-.*#.*");
		setThreads(10);
	getConf().setTopN(100);
	}
	@Override
	public void visit(Page page,CrawlDatums next) {
		 if (page.matchUrl("https://300report.jumpw.com/match.html[?]id=[0-9]{8}")) {
		   Document doc = page.doc();  
			String title=doc.select("div.datamsg").text();
			ArrayList<String>li=new ArrayList<String>();
			Elements tables=doc.getElementsByClass("datatable");
			for(Element table:tables)
		{
			Elements lists=table.select("tbody>tr");		
			for(Element list:lists)
			{  if(list!=lists.first())
				{
				String[] code=list.select("td:nth-child(2)").text().split(" ");//暂无法精确到<br>
				String kiara=code[code.length-1];
				String win=list.select("td:nth-child(4)").text();
				if(win.equals("首胜"))win="胜利";//可以在逻辑上(上面的必是胜利方)省略
				//逃跑人员和Solo对局过少不考虑在内
				li.add(getkiara(kiara)+" "+win);
				}
			}
		}
			map.put(title.split(" ")[2].substring(5).concat(" ").concat(title.split(" ")[3]).concat(" ").concat(String.valueOf((int)(Math.random()*100))),li);
			//添加随机数防止重复KEY
			try {
				FileWriter outdata=new FileWriter("D:\\\\serve\\out.txt");
				PrintWriter outfile=new PrintWriter(outdata);
				outfile.println(churi(map));
				outfile.flush();
			} catch (IOException e) {	
				e.printStackTrace();
			}
	}
	}
	public String churi (HashMap<String, ArrayList<String>> map2)
	{
		StringBuffer fin=new StringBuffer();;
	 for(String key:map2.keySet())
		 fin.append(key).append(map2.get(key)).append("\r\n"); 
		return fin.toString();
	}
	public String getkiara(String kiara)
	{
		for(int i=0;i<kiara.length();i++)
			if(kiara.charAt(i)=='(')
				return kiara.substring(0,i);
		return kiara;
	}
}
